import React, { useEffect } from "react";
import { Modal, Form, Input, Button } from "antd";

interface ResetPasswordModalProps {
  title: string;
  defaultValue?: any;
  visible: boolean;
  onCancel: () => void;
  onOk: (values: any) => void;
}

const ResetPasswordModal: React.FC<ResetPasswordModalProps> = ({
  title,
  defaultValue,
  visible,
  onCancel,
  onOk,
}) => {
  const [form] = Form.useForm();

  useEffect(() => {
    form.resetFields();
  }, [visible, form]);

  const handleOk = async () => {
    try {
      const values = await form.validateFields();
      onOk(values);
    } catch (errorInfo) {
      console.log("Failed:", errorInfo);
    }
  };

  return (
    <Modal
      title={title}
      visible={visible}
      onOk={handleOk}
      onCancel={onCancel}
      footer={[
        <Button key="cancel" onClick={onCancel}>
          取消
        </Button>,
        <Button key="ok" type="primary" onClick={handleOk}>
          确定
        </Button>,
      ]}
    >
      <Form
        form={form}
        initialValues={{
          ...defaultValue,
        }}
      >
        <Form.Item
          label="新密码" // 设置标签文本
          name="new_password"
          rules={[
            {
              required: true,
              whitespace: true,
              message: "请输入新密码",
            },
          ]}
          validateTrigger="onBlur"
        >
          <Input.Password placeholder="请输入新密码" />
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default ResetPasswordModal;
